package com.hskn.hss.module.alarmHandleLog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.module.alarmHandleLog.domain.AlarmHandleLog;
import com.hskn.hss.module.alarmHandleLog.mapper.AlarmHandleLogMapper;
import com.hskn.hss.module.alarmHandleLog.service.IAlarmHandleLogService;
import com.hskn.hss.module.alarmHandleLog.to.AlarmHandleLogListTO;
import com.hskn.hss.module.alarmHandleLog.vo.AlarmHandleLogVO;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.alarmsheshi.mapper.AlarmSheshiMapper;
import com.hskn.hss.module.messageFlowRecords.domain.MessageFlowRecords;
import com.hskn.hss.module.messageFlowRecords.service.IMessageFlowRecordsService;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.service.ITlCarService;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.service.ITlFacilitiesService;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.user.service.impl.UserServiceImpl;
import com.hskn.hss.module.violationHandleLog.domain.ViolationHandleLog;
import com.hskn.hss.module.violationHandleLog.mapper.ViolationHandleLogMapper;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import com.hskn.hss.utils.UpdateCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 报警处理记录Service业务层处理
 * 
 * @author ccm
 * @date 2021-09-07
 */
@Service
public class AlarmHandleLogServiceImpl  extends ServiceImpl<AlarmHandleLogMapper, AlarmHandleLog> implements IAlarmHandleLogService
{
    @Resource
    private AlarmHandleLogMapper alarmHandleLogMapper;
    @Autowired
    private ITlEmployeeService employeeService;
    @Autowired
    private ITlCarService carService;
    @Autowired
    private ITlFacilitiesService facilitiesService;
    @Autowired
    private IMessageFlowRecordsService messageFlowRecordsService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AlarmPersonMapper alarmPersonMapper;
    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private AlarmSheshiMapper alarmSheshiMapper;
    @Resource
    private UserServiceImpl userService;

    /**
     * 查询报警处理记录
     * 
     * @param id 报警处理记录主键
     * @return 报警处理记录
     */
    @Override
    public AlarmHandleLog selectAlarmHandleLogById(String id)
    {
        return alarmHandleLogMapper.selectAlarmHandleLogById(id);
    }

    /**
     * 查询报警处理记录列表
     * 
     * @param alarmHandleLog 报警处理记录
     * @return 报警处理记录
     */
    @Override
    public List<AlarmHandleLog> selectAlarmHandleLogList(AlarmHandleLog alarmHandleLog)
    {
        return alarmHandleLogMapper.selectAlarmHandleLogList(alarmHandleLog);
    }
    /**
     * 查询报警处理记录列表
     *
     * @param alarmHandleLogListTO 报警处理记录
     * @return 报警处理记录
     */
    @Override
    public List<AlarmHandleLogVO> selectAlarmHandleLogListByAlarmId(AlarmHandleLogListTO alarmHandleLogListTO)
    {
        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<AlarmHandleLog> alarmHandleLogQueryWrapper=new QueryWrapper<>();
        alarmHandleLogQueryWrapper.lambda().eq(AlarmHandleLog::getAlarmId,alarmHandleLogListTO.getAlarmId());
        alarmHandleLogQueryWrapper.lambda().orderByDesc(AlarmHandleLog::getCreateTime);
        List<AlarmHandleLog> alarmHandleLogList=alarmHandleLogMapper.selectList(alarmHandleLogQueryWrapper);
        Map<String, User> userMap = userService.getUserMap();

        alarmHandleLogList.forEach(t->{
            User user = userMap.get(t.getDealUid());
            if (Objects.nonNull(user) && StringUtils.isNotEmpty(user.getUserphoto())){
                t.setDealUAvatar(user.getUserphoto());
            }
        });

        List<AlarmHandleLogVO> alarmHandleLogVOList= JSONObject.parseArray(JSONObject.toJSONString(alarmHandleLogList),AlarmHandleLogVO.class);
        Map<String, TlEmployee> employeeMap=employeeService.getEmpMap(new QueryWrapper<>());
        Map<String, TlCar> carMap=carService.getCarMap(new QueryWrapper<>());

        AlarmPerson alarmPerson = alarmPersonMapper.selectById(alarmHandleLogListTO.getAlarmId());
        if (Objects.nonNull(alarmPerson)){
            MessageFlowRecords messageFlowRecords = new MessageFlowRecords();
            messageFlowRecords.setMessageId(alarmPerson.getAlarmPersonId());
            messageFlowRecords.setKind(1);
            messageFlowRecords.setSubjectType(1);
            messageFlowRecords.setUserId(uid);
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsService.selectMessageFlowRecordsList(messageFlowRecords);
            if (Objects.isNull(messageFlowRecords1) || messageFlowRecords1.size() == 0){
                messageFlowRecords.setCreateTime(new Date());
                messageFlowRecords.setId(StringUtils.uuid());
                messageFlowRecords.setSubjuctId(alarmPerson.getPersonId());
                messageFlowRecords.setSubjuctName(alarmPerson.getPersonName());
                messageFlowRecords.setContent(alarmPerson.getContent());
                User user=userMapper.selectById(uid);
                if (user!=null) {
                    messageFlowRecords.setUserName(user.getName());
                }
                messageFlowRecordsService.insertMessageFlowRecordsOnly(messageFlowRecords);
            }
        }

        AlarmCar alarmCar = alarmCarMapper.selectById(alarmHandleLogListTO.getAlarmId());
        if (Objects.nonNull(alarmCar)){
            MessageFlowRecords messageFlowRecords = new MessageFlowRecords();
            messageFlowRecords.setMessageId(alarmCar.getAlarmCarId());
            messageFlowRecords.setKind(1);
            messageFlowRecords.setSubjectType(2);
            messageFlowRecords.setUserId(uid);
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsService.selectMessageFlowRecordsList(messageFlowRecords);
            if (Objects.isNull(messageFlowRecords1) || messageFlowRecords1.size() == 0){
                messageFlowRecords.setCreateTime(new Date());
                messageFlowRecords.setId(StringUtils.uuid());
                messageFlowRecords.setSubjuctId(alarmCar.getCarId());
                messageFlowRecords.setSubjuctName(alarmCar.getCarName());
                messageFlowRecords.setContent(alarmCar.getContent());
                User user=userMapper.selectById(uid);
                if (user!=null) {
                    messageFlowRecords.setUserName(user.getName());
                }
                messageFlowRecordsService.insertMessageFlowRecordsOnly(messageFlowRecords);
            }
        }

        AlarmSheshi alarmSheshi = alarmSheshiMapper.selectById(alarmHandleLogListTO.getAlarmId());
        if (Objects.nonNull(alarmSheshi)){
            MessageFlowRecords messageFlowRecords = new MessageFlowRecords();
            messageFlowRecords.setMessageId(alarmSheshi.getAlarmSheshiId());
            messageFlowRecords.setKind(1);
            messageFlowRecords.setSubjectType(3);
            messageFlowRecords.setUserId(uid);
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsService.selectMessageFlowRecordsList(messageFlowRecords);
            if (Objects.isNull(messageFlowRecords1) || messageFlowRecords1.size() == 0){
                messageFlowRecords.setCreateTime(new Date());
                messageFlowRecords.setId(StringUtils.uuid());
                messageFlowRecords.setSubjuctId(alarmSheshi.getSheshiId());
                messageFlowRecords.setSubjuctName(alarmSheshi.getSheshiName());
                messageFlowRecords.setContent(alarmSheshi.getContent());
                User user=userMapper.selectById(uid);
                if (user!=null) {
                    messageFlowRecords.setUserName(user.getName());
                }
                messageFlowRecordsService.insertMessageFlowRecordsOnly(messageFlowRecords);
            }
        }

        for(AlarmHandleLogVO alarmHandleLogVO:alarmHandleLogVOList){




            if (alarmHandleLogVO.getViolationSign().equals("emp")){
               if(employeeMap.containsKey(alarmHandleLogVO.getSubjectId())){
                   alarmHandleLogVO.setSubjectHeadPortrait(employeeMap.get(alarmHandleLogVO.getSubjectId()).getPersonphoto());
               }
            }else if (alarmHandleLogVO.getViolationSign().equals("car")){
                if(carMap.containsKey(alarmHandleLogVO.getSubjectId())){
                    alarmHandleLogVO.setSubjectHeadPortrait(carMap.get(alarmHandleLogVO.getSubjectId()).getCarpicture());
                }
            }else if (alarmHandleLogVO.getViolationSign().equals("fac")){
                Map<String, TlFacilities> facilitiesMap=facilitiesService.getFacMap(new QueryWrapper<>());
                if(facilitiesMap.containsKey(alarmHandleLogVO.getSubjectId())){
                    alarmHandleLogVO.setSubjectHeadPortrait(facilitiesMap.get(alarmHandleLogVO.getSubjectId()).getFileurl());
                }
            }
            if (alarmHandleLogVO.getViolationTime()!=null) {
                alarmHandleLogVO.setViolationTimeString(Tools.getDateString(alarmHandleLogVO.getViolationTime()));
            }
        }
        return alarmHandleLogVOList;
    }

    /**
     * 新增报警处理记录
     * 
     * @param alarmHandleLog 报警处理记录
     * @return 结果
     */
    @Override
    public int insertAlarmHandleLog(AlarmHandleLog alarmHandleLog)
    {
        alarmHandleLog.setCreateTime(new Date());
        return alarmHandleLogMapper.insertAlarmHandleLog(alarmHandleLog);
    }

    /**
     * 修改报警处理记录
     * 
     * @param alarmHandleLog 报警处理记录
     * @return 结果
     */
    @Override
    public int updateAlarmHandleLog(AlarmHandleLog alarmHandleLog)
    {
        return alarmHandleLogMapper.updateAlarmHandleLog(alarmHandleLog);
    }

    /**
     * 批量删除报警处理记录
     * 
     * @param ids 需要删除的报警处理记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmHandleLogByIds(String[] ids)
    {
        return alarmHandleLogMapper.deleteAlarmHandleLogByIds(ids);
    }

    /**
     * 删除报警处理记录信息
     * 
     * @param id 报警处理记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmHandleLogById(String id)
    {
        return alarmHandleLogMapper.deleteAlarmHandleLogById(id);
    }
}
